using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ink;

namespace MVExplorer
{

	public partial class MVELogon : System.Windows.Window
	{
		public event EventHandler LogonExecuted;
		public event EventHandler LogonCancelled;
		public event EventHandler FadeOuted;

		private DoubleAnimation fadeInAnimation;
		private DoubleAnimation fadeOutAnimation;
		private DoubleAnimation fadeInNumpadAnimation;
		private DoubleAnimation fadeOutNumpadAnimation;

		private bool fEnableUI = true;

		private NumberPad numpad = new NumberPad();

		public MVELogon()
		{
			InitializeComponent();

			fadeInAnimation = (DoubleAnimation)this.Resources["FadeInAnimation"];
			fadeOutAnimation = (DoubleAnimation)this.Resources["FadeOutAnimation"];
			fadeOutAnimation.Completed += new EventHandler(fadeInAnimation_Completed);

			fadeInNumpadAnimation = (DoubleAnimation)this.Resources["FadeInNumpadAnimation"];
			fadeOutNumpadAnimation = (DoubleAnimation)this.Resources["FadeOutNumpadAnimation"];
			fadeOutNumpadAnimation.Completed += new EventHandler(fadeOutNumpadAnimation_Completed);

			LogonGrid.MouseLeftButtonDown += new MouseButtonEventHandler(MVELogon_MouseLeftButtonDown);
			this.Loaded += new RoutedEventHandler(MVELogon_Loaded);
			this.Activated += new EventHandler(MVELogon_Activated);
			this.Opacity = 0;
			this.AllowsTransparency = true;

			cmdLogon.Click += new RoutedEventHandler(cmdLogon_Click);
			cmdCancel.Click += new RoutedEventHandler(cmdCancel_Click);
			labelMessage.Opacity = 0;

			textUserName.StylusUp += new StylusEventHandler(textUserName_StylusUp);

			textUserName.GotFocus += new RoutedEventHandler(textUserName_GotFocus);
			textPassword.GotFocus += new RoutedEventHandler(textPassword_GotFocus);
			textPassword.LostFocus += new RoutedEventHandler(textPassword_LostFocus);
			
			textUserName.KeyUp += new KeyEventHandler(textUserName_KeyUp);
			textPassword.KeyUp += new KeyEventHandler(textPassword_KeyUp);

			
			spanelLogonTools.Children.Add(numpad);
			numpad.NumClicked += new NumClickEventHandler(numpad_NumClicked);
			numpad.BackspaceClicked += new EventHandler(numpad_BackspaceClicked);
			numpad.OkClicked += new EventHandler(numpad_OkClicked);

			//inkCnvsRecognize.DefaultDrawingAttributes.Color = Colors.DodgerBlue;
		}

		void MVELogon_Loaded(object sender, RoutedEventArgs e)
		{
			if (textUserName.Text == "")
				textUserName.Focus();
			else
				textPassword.Focus();

		}

		void textPassword_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
				Ok();
		}

		void textUserName_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
				Ok();
		}

		void textUserName_GotFocus(object sender, RoutedEventArgs e)
		{
			
			InputMethod im = InputMethod.Current;
			im.ImeState = InputMethodState.Off;

			//string str = RecognizeInkString();
			//if (!string.IsNullOrEmpty(str))
			//	textUserName.SelectedText = str.ToLower();
		}


		void textUserName_StylusUp(object sender, StylusEventArgs e)
		{
		}

        /*--
		private string RecognizeInkString()
		{
			if (inkCnvsRecognize.Strokes.Count <= 0)
				return "";
			InkAnalyzer analyzer = new InkAnalyzer();
			analyzer.AddStrokes(inkCnvsRecognize.Strokes);
			AnalysisStatus status = analyzer.Analyze();

			if (status.Successful)
			{
				string strRecognized = analyzer.GetRecognizedString();
				inkCnvsRecognize.Strokes.Clear();
				return strRecognized;
			}
			return "";
		}
         --*/

		private void FadeIn()
		{
			if (this.Opacity == 0)
				this.BeginAnimation(Frame.OpacityProperty, fadeInAnimation, HandoffBehavior.SnapshotAndReplace);
		}

		public void FadeOut()
		{
			if (this.Opacity > 0)
				this.BeginAnimation(Frame.OpacityProperty, fadeOutAnimation, HandoffBehavior.SnapshotAndReplace);
		}

		void fadeInAnimation_Completed(object sender, EventArgs e)
		{
			this.Close();
			if (FadeOuted != null)
			{
				FadeOuted(this, new EventArgs());
			}
		}

		void MVELogon_Activated(object sender, EventArgs e)
		{
			FadeIn();
		}

		void MVELogon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			this.DragMove();
		}

		private void Ok()
		{
			if (LogonExecuted != null)
				LogonExecuted(this, new EventArgs());
		}

		void cmdLogon_Click(object sender, RoutedEventArgs e)
		{
			Ok();
		}

		void cmdCancel_Click(object sender, RoutedEventArgs e)
		{
			if (LogonCancelled != null)
				LogonCancelled(this, new EventArgs());
		}

		internal string UserName
		{
			get { return textUserName.Text; }
			set { textUserName.Text = value; }
		}

		internal string Password
		{
			get { return textPassword.Password; }
			set { textPassword.Password = value; }
		}

		public string Message
		{
			get { return labelMessage.Content.ToString(); }
			set
			{
				labelMessage.Content = value;
                labelMessage.Foreground = Brushes.DarkRed;
				labelMessage.Opacity = 0.8;
				this.InvalidateVisual();
			}
		}

		public bool EnableUI
		{
			get { return fEnableUI; }
			set
			{
				fEnableUI = value;
				textUserName.IsEnabled = fEnableUI;
				textPassword.IsEnabled = fEnableUI;
				cmdLogon.IsEnabled = fEnableUI;
				cmdCancel.IsEnabled = fEnableUI;
			}
		}

		#region Numpad Methods

		void numpad_OkClicked(object sender, EventArgs e)
		{
			Ok();
		}

		void numpad_BackspaceClicked(object sender, EventArgs e)
		{
			if (textPassword.Password.Length > 0)
				textPassword.Password = textPassword.Password.Substring(0, textPassword.Password.Length - 1);
		}

		void numpad_NumClicked(object sender, string number)
		{
			textPassword.Password += number;
		}

		void textPassword_LostFocus(object sender, RoutedEventArgs e)
		{
			HideNumpad();
		}

		void textPassword_GotFocus(object sender, RoutedEventArgs e)
		{
			ShowNumpad();
		}

		private void ShowNumpad()
		{
			spanelLogonTools.Visibility = Visibility.Visible;
			if (spanelLogonTools.Opacity == 0)
				spanelLogonTools.BeginAnimation(Frame.OpacityProperty, fadeInNumpadAnimation, HandoffBehavior.SnapshotAndReplace);
		}

		private void HideNumpad()
		{
			if (spanelLogonTools.Opacity > 0)
				spanelLogonTools.BeginAnimation(Frame.OpacityProperty, fadeOutNumpadAnimation, HandoffBehavior.SnapshotAndReplace);
		}

		void fadeOutNumpadAnimation_Completed(object sender, EventArgs e)
		{
			spanelLogonTools.Visibility = Visibility.Hidden;
		}

		#endregion
	}
}